package com.example.nacos.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.nacos.cache.CacheData;
import com.example.nacos.entity.Goods;
import com.example.nacos.mapper.GoodsMapper;
import com.example.nacos.vo.GoodsCatalogListVo;
import com.example.nacos.vo.GoodsDetailVo;
import com.example.nacos.vo.GoodsSysCatalogVo;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * 拉取冠龙公司产品数据
 */
@RestController
@RequestMapping("dataPull")
public class YnhcDataPullController {
    @Autowired
    private GoodsMapper goodsMapper;
    /**
     * 系统分类
     */
    public static final String goodSysUrl = "https://ggfw.scyb.org.cn/hctrade/suppurProcurecatalogZt/getSpecialUsuallyProductZtList.html";
    /**
     * 产品分类
     */
    public static final String goodCatalogUrl = "https://ggfw.scyb.org.cn/hctrade/suppurProcurecatalogZt/getPartDetails.html";

    /**
     * 产品
     */
    public static final String goodDetailUrl = "https://ggfw.scyb.org.cn/hctrade/suppurProcurecatalogZt/getLookZtOutlookAndModel.html?productId=";

    public static ThreadLocal<String> sysCode = new ThreadLocal<>();


    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/pullGoodsSys")
    public void pullGoodsSys(@RequestHeader("cookie") String cookie,
                             @RequestHeader("nd") String nd,
                             @RequestParam(name = "companyName") String companyName,
                             @RequestParam(name = "sysId") String sysId) {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();

        params.add("companyNameTb", companyName);
        params.add("procurecatalogId", sysId);
        params.add("nd", nd);
        params.add("sord", "asc");
        params.add("page", 1);
        params.add("rows", 200);
        sysCode.set(sysId);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("cookie", cookie);
        HttpEntity entity = new HttpEntity(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(goodSysUrl, HttpMethod.POST, entity, String.class);
        String res = responseEntity.getBody();
        JSONObject resJson = JSON.parseObject(res, JSONObject.class);
        Object rows = resJson.get("rows");
        List<GoodsSysCatalogVo> list = JSONObject.parseArray(JSON.toJSONString(rows), GoodsSysCatalogVo.class);
        System.out.println(JSON.toJSONString(list));
        List<GoodsCatalogListVo> catalogListVos = getGoodsCatalogList(list, cookie, nd);
        getGoodsList(catalogListVos, cookie, nd);
        sysCode.remove();
    }

    private List<GoodsCatalogListVo> getGoodsCatalogList(List<GoodsSysCatalogVo> list, String cookie, String nd) {
        List<GoodsCatalogListVo> vos = new ArrayList<>();
        for (GoodsSysCatalogVo goodsSysCatalogVo : list) {
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();

            params.add("procurecatalogId", goodsSysCatalogVo.getProcurecatalogId());
            params.add("nd", nd);
            params.add("sord", "asc");
            params.add("page", 1);
            params.add("rows", 300);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.add("cookie", cookie);
            HttpEntity entity = new HttpEntity(params, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(goodCatalogUrl, HttpMethod.POST, entity, String.class);
            String res = responseEntity.getBody();
            JSONObject resJson = JSON.parseObject(res, JSONObject.class);
            Object rows = resJson.get("rows");

            List<GoodsCatalogListVo> goodsCatalogListVos = (List<GoodsCatalogListVo>) JSON.parseArray(JSON.toJSONString(rows), GoodsCatalogListVo.class);
            System.out.println("--------------------------------------------------------------------------------");
            System.out.println(JSON.toJSONString(goodsCatalogListVos));
            vos.addAll(goodsCatalogListVos);
        }
        return vos;
    }

    private void getGoodsList(List<GoodsCatalogListVo> list, String cookie, String nd) {
        for (GoodsCatalogListVo goods : list) {
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();

            params.add("procurecatalogId", goods.getProductId());
            params.add("nd", nd);
            params.add("sord", "asc");
            params.add("page", 1);
            params.add("rows", 2000);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.add("cookie", cookie);
            HttpEntity entity = new HttpEntity(params, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(goodDetailUrl + goods.getProductId(), HttpMethod.POST, entity, String.class);
            String res = responseEntity.getBody();
            JSONObject resJson = JSON.parseObject(res, JSONObject.class);
            Object rows = resJson.get("rows");

            List<Goods> goodsCatalogListVos = JSON.parseArray(JSON.toJSONString(rows), Goods.class);
            System.out.println("----------------------------detail----------------------------------------------------");
            System.out.println(JSON.toJSONString(goodsCatalogListVos));
            for (Goods goodsCatalogListVo : goodsCatalogListVos) {
                if (!CacheData.goodsMap.containsKey(goodsCatalogListVo.getGoodsCode())) {
                    //goodsCatalogListVo.setSysCode(sysCode.get());
                    goodsMapper.insert(goodsCatalogListVo);
                }
            }
        }
        initData();
    }

    @PostConstruct
    private void initData() {
        goodsMapper.selectList(null).forEach(ite -> {
            if (!CacheData.goodsMap.containsKey(ite.getGoodsCode())) {
                CacheData.goodsMap.put(ite.getGoodsCode(), ite);
            }
            if (!CacheData.goodsMapKeyYbm.containsKey(ite.getDrugCode())) {
                CacheData.goodsMapKeyYbm.put(ite.getDrugCode(), ite);
            }
        });
    }





}
